package com.yc.mvc.web;

import com.yc.mvc.Person;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/9
 * Email haijunzhou@hnit.edu.cn
 */
@Controller
@RequestMapping("abc")
public class DemoAction {

    @RequestMapping("toIdx")
    public String toIndex(){
        // 1.请求转发  2.响应重定向
        //return "forward:index.jsp";
        return "redirect:../index.html";  // ViewName
    }

    @RequestMapping("now")
    @ResponseBody  // @Controller + @ResponseBody
    Date now(){
        return new Date();
    }

    @RequestMapping(path = "/**/myname")
    @ResponseBody
    String myname(){
        return "zhangsan";
    }

    @PostMapping(value = "person",
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    Person person(Person person){
        return person;
    }

    @RequestMapping("servlet")
    @ResponseBody
    List<Object> servlet(HttpServletRequest req,
                         HttpServletResponse resp,
                         HttpSession session,
                         InputStream in,
                         OutputStream out){
        List<Object> ret = new ArrayList<>();
        ret.add(req.toString());
        ret.add(resp.toString());
        ret.add(session.toString());
        ret.add(in.toString());
        ret.add(out==null?null:out.toString());
        return ret;
    }



}
